#!/usr/bin/perl -w
use strict;
if($#ARGV + 1 < 2){
	die "Insufficient arguments\nUsage: var.pl <pileup-file-name> <wgsim-report-file>\n";
	}

open PILEUP_FILE_HANDLE, $ARGV[0] or die $!;
my @pile_up_contents = <PILEUP_FILE_HANDLE>;
#my $line_num = 0;
my %summary_sam;

foreach my $line_pileup(@pile_up_contents){
#gi|49175990|ref|NC_000913.2|	        1	               A	             2	            ^].^].	        22
#    $1		                   $2      $3         $4      $5       $6        $7      $8       $9     $10    $11
#   (.*)                     (\s+)    ([0-9]+)   (\s+)  (A|T|G|C) (\s+)   ([0-9]+)  (\s+)    (.*)    (\s+)  (.*)
#	$line_num++;
	$line_pileup =~ /(.*?)(\s+?)([0-9]+?)(\s+?)(A|T|G|C?)(\s+?)([0-9]+?)(\s+?)(.*?)(\s+?)(.*)/;
	#print "\$1: $1 \$2: $2 \$3: $3 \$4: $4 \$5: $5 \$6: $6 \$7: $7 \$8: $8 \$9: $9 \$10: $10 \$11: $11";
	my $read_base = $9;
	my $num = $3;
	if($9 eq ""){
		die "Error in string match at line_pileup: $num\n";
	}
	if($read_base =~ /(\+|\-)[0-9]+[A|C|G|T|N|a|c|g|t|n]+/){
		#print "In at line_pileup $num\n";
		my @reads = split //, $read_base;
		my $ctr = 0;
		my %dom_expr_count_pileup;
		while($ctr < scalar @reads){
			if($reads[$ctr] eq '+'){
				$ctr++;
				my $num_indel = $reads[$ctr];
				#$ctr++;
				my $inner_loop_ctr = 0;
				my $str = "+";
				while($inner_loop_ctr <= $num_indel){
					$str .= $reads[$ctr++];
					$inner_loop_ctr++;
				}
				$dom_expr_count_pileup{$str} ++;# $num_indel;
			}
			elsif($reads[$ctr] eq '-'){
				$ctr++;
				my $num_indel = $reads[$ctr];
				#$ctr++;
				my $inner_loop_ctr = 0;
				my $str = "-";
				while($inner_loop_ctr <= $num_indel){
					$str .= $reads[$ctr++];
					$inner_loop_ctr++;
				}
				$dom_expr_count_pileup{$str} ++;# $num_indel;
			}elsif($reads[$ctr] eq '.'){
				$dom_expr_count_pileup{"."} ++;
			}elsif($reads[$ctr] eq ','){
				$dom_expr_count_pileup{","} ++;
			}
			$ctr++;
		}
		
		my $dom_term_val = 0;
		my $dom_term_key = "";
		while ((my $key, my $value) = each(%dom_expr_count_pileup)){
#			print $key." => ".$value."\n";
			if($value ge $dom_term_val){
				$dom_term_val = $value;
				$dom_term_key = $key;
			}
		}
		my @temp_arr;
		push @temp_arr, $dom_term_key;
		push @temp_arr, $dom_term_val;
#		print "@temp_arr\n";
		$summary_sam{$num} = \@temp_arr;
		print "\$summary_sam{$num} = \@temp_arr\n";
#		print "In at line_pileup $num \$dom_term_val: $dom_term_val  \$dom_term_key: $dom_term_key\n";
#		<STDIN>;
	}
}

close PILEUP_FILE_HANDLE;

open WGSIM_REPORT_FILE_HANDLE,$ARGV[1] or die $!;
my @report_file_contents = <WGSIM_REPORT_FILE_HANDLE>;
my %summary_report;
foreach my $line_report(@report_file_contents){
		chomp($line_report);
		#print "** $line_report **\n";
		#$line_report =~ /(.*\|)(\s+)([0-9]+)(\s+)((A|T|G|C|-)+)(\s+)((A|G|C|T|-)+)(.*)/;
		$line_report =~ /(.*)(\s+)([0-9]+)(\s+)([ATGC-]+)(\s+)(([A-Z]|-)*)(.*)/;
		#print "## \$1: $1\t\$3: $3\t\$5: $5\t\$7: $7 ##\n";
		if(!defined $5){
			print "** $line_report **\n";
			last;
		}
		my $tmp;
		if($5 eq '-'){
			#print $7,"\n";
			my @splits = split //, $7;
			my $sp_len = scalar @splits;
			$tmp = '+'.$sp_len.$7;
			print "\$summary_report{$3} = $tmp\n";
			$summary_report{$3} = $tmp;
		}elsif($7 eq '-'){
			my @splits = split //, $5;
			my $sp_len = scalar @splits;
			$tmp = '-'.$sp_len.$5;
			$summary_report{$3} = $tmp;
			print "\$summary_report{$3} = $tmp\n";
		}
	}

close WGSIM_REPORT_FILE_HANDLE;

for my $key ( sort (keys %summary_report) ) {
	if(!defined(@{$summary_sam{$key}})){
		my @temp = @{$summary_sam{$key}};		
		if ($summary_report{$key} eq $temp[0]){
			print "match at $key [$summary_report{$key}, $temp[0]]\n";
		}else{
			print "variation at $key [$summary_report{$key}, $temp[0]]\n";
		}
	}
}


